#import "BWFrenchKnotSet.h"
#import "BWFrenchKnot.h"

@implementation BWFrenchKnotSet


- (id) init
{
    if ([super init] == nil) {
        return (nil);
    }

    knots = [[NSMutableSet alloc] init];

    return (self);

} // init


- (void) dealloc
{
    [knots release];
    [super dealloc];

} // dealloc


- (void) addKnot: (BWFrenchKnot *) knot
{
    [knots addObject: knot];
} // addKnot


- (NSEnumerator *) objectEnumerator
{
    return ([knots objectEnumerator]);
} // objectEnumerator


- (BWFrenchKnot *) knotAtX: (int) x  y: (int) y
{
    BWFrenchKnot *foundKnot = nil;

    NSEnumerator *enumerator = [self objectEnumerator];

    BWFrenchKnot *knot;
    while (knot = [enumerator nextObject]) {
        if ([knot x] == x && [knot y] == y) {
            foundKnot = knot;
            break;
        }
    }

    return (foundKnot);

} // knotAtX


- (void) removeKnot: (BWFrenchKnot *) knot
{
    if (knot != nil) {
        [knots removeObject: knot];
    }

} // removeKnot


- (void) shiftKnotsHorizontally: (int) hcount
                     vertically: (int) vcount
{
    NSEnumerator *enumerator = [self objectEnumerator];

    BWFrenchKnot *knot;
    while (knot = [enumerator nextObject]) {
        int x, y;
        x = [knot x];
        y = [knot y];
        [knot setX: x + hcount];
        [knot setY: y + vcount];
    }

} // shiftKnotsHorizontally


- (void) encodeWithCoder: (NSCoder *) coder
{
    [coder encodeObject: knots  forKey: @"knotSet"];

} // encodeWithCoder


- (id) initWithCoder: (NSCoder *) coder
{
    if (self = [super init]) {
        knots = [coder decodeObjectForKey: @"knotSet"];
        [knots retain];
    }

    return (self);

} // initWithCoder

@end // BWFrenchKnotSet
